package cn.south.campus.business.model.vo;

import cn.south.campus.business.model.entity.Goods;
import cn.south.campus.common.model.vo.BaseVO;
import cn.south.campus.common.utils.DateUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.math.BigDecimal;

/**
 * <p>
 *
 * </p>
 *
 * @author chow
 * @since 2024/11/14 10:26
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class GoodsVo extends BaseVO {

    /**
     * 分类id
     */
    private Long categoryId;

    /**
     * 分类名称
     */
    private String categoryName;

    /**
     * 商品名称
     */
    private String name;

    /**
     * 价格
     */
    private String price;

    /**
     * 图片地址
     */
    private String url;

    /**
     * 发布人
     */
    private Long publisher;

    /**
     * 发布人
     */
    private String nickname;

    /**
     * 发布人头像
     */
    private String avatar;

    /**
     * 发布人手机号
     */
    private String phone;

    /**
     * 总库存
     */
    private Integer stock;

    /**
     * 剩余数量
     */
    private Integer remain;

    /**
     * 描述
     */
    private String description;

    /**
     * 状态 0下架 1上架
     */
    private Integer status;

    /**
     * 发布时间
     */
    private String pubTime;

    /**
     * 发布时间
     */
    private String timeAgo;

    /**
     * 上架时间
     */
    private String onSaleTime;

    public static GoodsVo convert(Goods goods) {
        if (goods == null) {
            return null;
        }
        GoodsVo goodsVo = new GoodsVo();
        goodsVo.setId(goods.getId());
        goodsVo.setCategoryId(goods.getCategoryId());
        goodsVo.setCategoryName(goods.getCategoryName());
        goodsVo.setName(goods.getName());
        goodsVo.setPrice(goods.getPrice().toString());
        goodsVo.setUrl(goods.getUrl());
        goodsVo.setPublisher(goods.getPublisher());
        goodsVo.setNickname(goods.getPublisherName());
        goodsVo.setAvatar(goods.getPublisherAvatar());
        goodsVo.setPhone(goods.getPublisherPhone());
        goodsVo.setRemain(goods.getRemain());
        goodsVo.setStock(goods.getStock());
        goodsVo.setDescription(goods.getDescription());
        goodsVo.setStatus(goods.getStatus());
        goodsVo.setCreateTime(DateUtil.format(goods.getCreateTime()));
        goodsVo.setUpdateTime(DateUtil.format(goods.getUpdateTime()));
        goodsVo.setPubTime(DateUtil.format(goods.getPubTime()));
        if (goods.getOnSaleTime() != null) {
            goodsVo.setOnSaleTime(DateUtil.format(goods.getOnSaleTime()));
            goodsVo.setTimeAgo(DateUtil.getTimeAgo(goods.getOnSaleTime()));
        }else {
            goodsVo.setTimeAgo(DateUtil.getTimeAgo(goods.getUpdateTime()));
        }
        return goodsVo;
    }

    public static Goods convert(GoodsVo goodsVo) {
        if (goodsVo == null) {
            return null;
        }
        Goods goods = new Goods();
        goods.setCategoryId(goodsVo.getCategoryId());
        goods.setName(goodsVo.getName());
        goods.setPrice(new BigDecimal(goodsVo.getPrice()));
        goods.setUrl(goodsVo.getUrl());
        goods.setPublisher(goodsVo.getPublisher());
        goods.setStock(goodsVo.getStock());
        goods.setRemain(goodsVo.getRemain());
        goods.setDescription(goodsVo.getDescription());
        goods.setStatus(goodsVo.getStatus());
        goods.setPubTime(DateUtil.parseString(goodsVo.getPubTime()));
        goods.setOnSaleTime(DateUtil.parseString(goodsVo.getOnSaleTime()));
        goods.setId(goodsVo.getId());
        return goods;
    }
}
